package com.rc.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rc.activity.DashBoardScreen;
import com.rc.activity.R;
import com.rc.activity.dao.tbl_Trip;
import com.rc.activity.dao.tbl_TripItems;
import com.rc.activity.provider.Contract;
import com.rc.db.DatabaseSupport;
import com.rc.fragment.TripDetailFragment;
import com.rc.util.Util;

public class TripListAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<tbl_Trip> items;
	public boolean isShowCheck = false;
	private CheckBox cbMulti;
	private String mTripKey;
	private int start, end;

	public TripListAdapter(String mTripKey, Context c,
			ArrayList<tbl_Trip> item, int start, int end) {
		mContext = c;
		this.items = item;
		this.mTripKey = mTripKey;
		this.end = end;
		this.start = start;
	}

	public int getCount() {
		return items.size();
	}

	public Object getItem(int position) {
		return items.get(position);
	}

	public long getItemId(int position) {
		return 0;
	}

	// create a new view for each item referenced by the Adapter
	@SuppressLint("ResourceAsColor")
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = convertView;
		ImageView imgView;
		TextView txtTripName, txtTime, txtMoney, txtMoneyClamer, txtTripType;
		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			LayoutInflater vi = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.triplist_item, null);

		}
		imgView = (ImageView) v.findViewById(R.id.imgView);
		cbMulti = (CheckBox) v.findViewById(R.id.cbMulti);
		txtTripName = (TextView) v.findViewById(R.id.txtTripName);
		txtMoney = (TextView) v.findViewById(R.id.txtMoney);
		txtMoneyClamer = (TextView) v.findViewById(R.id.txtMoneyClamer);
		txtTime = (TextView) v.findViewById(R.id.txtTime);
		txtTripType = (TextView) v.findViewById(R.id.txtTripType);

		if (mContext.getString(R.string.current_trips).equals(
				items.get(position).getType())) {
			txtTripType.setText("C");
		} else if (mContext.getString(R.string.past_trips).equals(
				items.get(position).getType())) {
			txtTripType.setText("P");
		} else if (mContext.getString(R.string.upcoming_trips).equals(
				items.get(position).getType())) {
			txtTripType.setText("U");
		}

		if (!isShowCheck) {
			cbMulti.setVisibility(View.GONE);
		} else {
			cbMulti.setVisibility(View.VISIBLE);
		}

		txtTripName.setText(items.get(position).getName());

		// replace start date "-" from "/"
		String startDate = items.get(position).getStartDate();
		startDate = startDate.replace("-", "/");

		// replace end date "-" from "/"
		String endDate = items.get(position).getEndDate();
		endDate = endDate.replace("-", "/");

		// set text UI
		txtTime.setText(startDate + "-" + endDate);
		cbMulti.setChecked(items.get(position).isCheck());
		String textMoneyClaimer = "";
		if (items.get(position).getReport_ID() != 0) {
			float claim = 0, approver = 0;
			ArrayList<tbl_TripItems> arrTripItems = new ArrayList<tbl_TripItems>();
			arrTripItems = DatabaseSupport.getTripItemsByTripID(
					items.get(position).getTripID(), mContext);
			for (tbl_TripItems objTripItems : arrTripItems) {
				claim = claim + objTripItems.getClaimed();
				approver = approver + objTripItems.getApproved();
			}
			if (items.get(position).getIsClaimed() == 1) {
				int isApproved = items.get(position).getIsApproved();
				switch (isApproved) {
				case 0:
					textMoneyClaimer = claim + "";
					break;
				case 1:
					textMoneyClaimer = approver + "";
					break;
				case 2:
					textMoneyClaimer = claim + "";
					break;
				default:
					break;
				}
			}
		}
		if ("".equals(textMoneyClaimer)) {
			textMoneyClaimer = "0.00";
		}
		// setBackground
		if (items.get(position).getIsCreate() != null) {
			if (items.get(position).getIsCreate().equals("1")) {
				v.setBackgroundResource(R.color.background_trip_list_not_sync);
				imgView.setVisibility(View.VISIBLE);
				txtTripType.setVisibility(View.GONE);
			}
		}

		txtMoney.setText("$"
				+ Util.formatStringFloat(Util.convertFloat(items.get(position)
						.getAmount())));
		txtMoneyClamer.setText("$"
				+ Util.formatStringFloat(Util.convertFloat(textMoneyClaimer)));

		v.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (isShowCheck) {
					cbMulti = null;
					cbMulti = ((CheckBox) v.findViewById(R.id.cbMulti));
					if (cbMulti.isChecked()) {
						cbMulti.setChecked(false);
						items.get(position).setCheck(false);
					} else {
						cbMulti.setChecked(true);
						items.get(position).setCheck(true);
					}
				} else {
					if (items.get(position).getIsCreate().equals("1")) {
						createDialogError();
					} else {
						Fragment fragment = new TripDetailFragment(mTripKey,
								items.get(position).getTripID(), true, 0, "",
								start, end);
						Util.pushFragmentReceiptBox(fragment,
								(FragmentActivity) mContext, "2");
					}

				}
			}
		});
		return v;
	}

	public void notiB(ArrayList<tbl_Trip> arrTripObj, String mTripKey,
			int start, int end) {
		this.items = arrTripObj;
		this.mTripKey = mTripKey;
		this.start = start;
		this.end = end;
		notifyDataSetChanged();
	}

	private void createDialogError() {
		final Dialog dialog = new Dialog(mContext);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_message);

		LinearLayout llOk, llCancel;
		((TextView) dialog.findViewById(R.id.txtTitle)).setText(mContext
				.getString(R.string.messages));
		((TextView) dialog.findViewById(R.id.txtContent)).setText(mContext
				.getString(R.string.trip_is_being_verified));

		llOk = (LinearLayout) dialog.findViewById(R.id.llOk);

		llCancel = (LinearLayout) dialog.findViewById(R.id.llCancel);
		llCancel.setVisibility(View.GONE);
		llOk.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ContentResolver.requestSync(DashBoardScreen.mConnectedAccount,
						Contract.AUTHORITY, new Bundle());
				dialog.dismiss();
			}
		});

		dialog.show();

	}
}